proc find_nearby { type entity radius args } {
	*clearmarkall 1
	*clearmarkall 2
	*createmark $type 1 $entity
	hm_getnearbyentities inputentitytype=$type inputentitymark=1 outputentitytypes={$type} outputentitymark=2 radius=[expr {max(2*$radius,20)}] nearby_search_method=sphere
	return [ hm_getmark $type 2 ]
}

proc creat_weld_from_components { {search_radius 10} {exclude_holes_with_radius_less_than 20} args } {
	*createmarkpanel components 1 "Please select the components"
	set comps_list [ hm_getmark components 1 ]
	*clearmarkall 1
	set n_comp [ llength $comps_list ]
	set i 1
	set comp1_ok ""
	foreach comp1 $comps_list {
		puts "$i/$n_comp"
		puts "comp1 : $comp1"
		lappend comp1_ok $comp1
		*clearmarkall 1
		*clearmarkall 2

		set comps_nearby [ find_nearby comps $comp1 $search_radius ]
		
		eval *createmark comps 1  $comps_list
		eval *createmark comps 2  $comps_nearby		
		*markintersection comps 1 comps 2
		
		eval *createmark comps 2  $comp1_ok
		
		*markdifference comps 1 comps 2
		set comp2_list [ hm_getmark comps 1 ]
		
		puts "comp2 : $comp2_list"
		foreach comp2 $comp2_list {
			puts "for comp1 : $comp1 ; comp2 : $comp2"
			*createmark components 1  $comp1 $comp2
			*CE_ConnectorCreateByAutoseam components 1 "search_radius = $search_radius pitch = 30.000000 minimum_connector_length = 10.000000 exclude_holes_with_radius_less_than = $exclude_holes_with_radius_less_than create_internal_seams = 0"
		}
		incr i
	}
}


proc creat_weld_from_parts { {search_radius 10} {exclude_holes_with_radius_less_than 20} args } {
	*createmarkpanel parts 1 "Please select the parts"
	set parts_list [ hm_getmark parts 1 ]
	*clearmarkall 1
	set n_part [ llength $parts_list ]
	set i 1
	set part1_ok ""
	foreach part1 $parts_list {
		puts "$i/$n_part"
		puts "part1 : $part1"
		lappend part1_ok $part1
		*clearmarkall 1
		*clearmarkall 2
		
		set parts_nearby [ find_nearby parts $part1 $search_radius ]
		eval *createmark parts 1  $parts_list
		eval *createmark parts 2  $parts_nearby		
		*markintersection parts 1 parts 2
		set tt [ hm_getmark parts 1 ]
		eval *createmark parts 2  $part1_ok
		*markdifference parts 1 parts 2
		set part2_list [ hm_getmark parts 1 ]
		
		puts "part2 : $part2_list"
		foreach part2 $part2_list {
			puts "for part1 : $part1 ; part2 : $part2"
			*createmark parts 1  $part1 $part2
			*CE_ConnectorCreateByAutoseam parts 1 "search_radius = $search_radius pitch = 30.000000 minimum_connector_length = 10.000000 exclude_holes_with_radius_less_than = $exclude_holes_with_radius_less_than create_internal_seams = 0"
		}
		incr i
	}
}


proc creat_weld_between_parts { {search_radius 10} {exclude_holes_with_radius_less_than 20} args } {
	*createmarkpanel parts 1 "Please select the parts 1"
	set parts_list_A [ hm_getmark parts 1 ]
	*clearmarkall 1
	*createmarkpanel parts 1 "Please select the parts 2"
	set parts_list_B [ hm_getmark parts 1 ]
	*clearmarkall 1
	set n_part [ llength $parts_list_A ]
	set i 1
	set part1_ok ""
	foreach part1 $parts_list_A {
		puts "$i/$n_part"
		puts "part1 : $part1"
		lappend part1_ok $part1
		*clearmarkall 1
		*clearmarkall 2
		
		set parts_nearby [ find_nearby parts $part1 $search_radius ]
		eval *createmark parts 1  $parts_list_B
		eval *createmark parts 2  $parts_nearby		
		*markintersection parts 1 parts 2
		set tt [ hm_getmark parts 1 ]
		eval *createmark parts 2  $part1_ok
		*markdifference parts 1 parts 2
		set part2_list [ hm_getmark parts 1 ]
		
		puts "part2 : $part2_list"
		foreach part2 $part2_list {
			puts "for part1 : $part1 ; part2 : $part2"
			*createmark parts 1  $part1 $part2
			*CE_ConnectorCreateByAutoseam parts 1 "search_radius = $search_radius pitch = 30.000000 minimum_connector_length = 10.000000 exclude_holes_with_radius_less_than = $exclude_holes_with_radius_less_than create_internal_seams = 0"
		}
		incr i
	}
}